package gin

import (
	"fmt"
	"time"
)

const defaultSlowThreshold = time.Second * 3

// Config HTTP config
type Config struct {
	Host       string
	Port       uint64
	Deployment string
	Mode       string

	DisableMetric bool
	DisableTrace  bool

	SlowThreshold time.Duration
}

// Build create server instance, then initialize it with necessary interceptor
func (config *Config) Build() *Server {
	if config.Mode == "" {
		config.Mode = "debug"
	}
	if config.SlowThreshold == 0 {
		config.SlowThreshold = defaultSlowThreshold
	}
	server := newServer(config)
	server.Use()
	server.Use(recoverMiddleware(config.SlowThreshold))

	if !config.DisableMetric {
		server.Use(metricServerInterceptor())
	}

	if !config.DisableTrace {
		server.Use(traceServerInterceptor())
	}
	return server
}

// Address ...
func (config *Config) Address() string {
	return fmt.Sprintf("%s:%d", config.Host, config.Port)
}
